package com.mall.user.mq;

import com.alibaba.fastjson.JSON;
import com.mall.user.bo.MailInfoBO;
import com.mall.user.dto.UserRegisterRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Component
@Slf4j
public class MailConsumer {
    DefaultMQPushConsumer consumer;

    @Autowired
    JavaMailSender mailSender;

    @PostConstruct
    public void  init(){
        consumer = new DefaultMQPushConsumer("mail_consumer");
        consumer.setNamesrvAddr("127.0.0.1:9876");
        consumer.setMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                try {
                    MessageExt message = msgs.get(0);
                    sendEmail(message);
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error("com.mall.user.mq.MailConsumer.init occur exception" + e);
                    return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });

        try {
            consumer.subscribe("mail_producer","*");
            consumer.start();
        } catch (MQClientException e) {
            e.printStackTrace();
            log.info("com.mall.user.mq.MailConsumer.init occur exception" + e);
        } catch (Exception e){
            e.printStackTrace();
            log.error("com.mall.user.mq.MailConsumer.init occur exception" + e);
        }
    }


    //发送用户激活邮件
    private void sendEmail(MessageExt messageExt) {
        try {
            byte[] body = messageExt.getBody();
            String data = new String(body, 0, body.length, "utf-8");
            MailInfoBO mailInfoBO = JSON.parseObject(data, MailInfoBO.class);
            String uuid = mailInfoBO.getUuid();
            UserRegisterRequest registerRequest = mailInfoBO.getUserRegisterRequest();

            SimpleMailMessage message = new SimpleMailMessage();
            //TODO：优化：写入配置文件中获取信息
            message.setSubject("CSMALL用户激活");
            message.setFrom("ciggarnot@163.com");
            message.setTo(registerRequest.getEmail());
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("http://localhost:8080/user/verify?uid=").append(uuid).append("&username=")
                    .append(registerRequest.getUserName());
            message.setText(stringBuilder.toString());
            mailSender.send(message);
            System.out.println("用户邮件已发送");
        } catch (MailException e) {
            e.printStackTrace();
            log.error("com.mall.user.mq.MailConsumer.sendEmail occur exception:" + e);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            log.error("com.mall.user.mq.MailConsumer.sendEmail occur exception:" + e);
        } catch (Exception e){
            e.printStackTrace();
            log.error("com.mall.user.mq.MailConsumer.sendEmail occur exception:" + e);
        }
    }
}
